﻿using LiteBlog.Common.Entities;
using LiteBlog.Components;
using LiteBlog.Components.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LiteBlog.Controllers.Filters
{
    public class PostNotFoundFilter : ActionFilterAttribute 
    {        
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {       
                string id2;
                string id = filterContext.ActionParameters["id"].ToString();
                var controller = filterContext.Controller as PostController;
                var blogComponent = controller.BlogComponent;
                if (!blogComponent.PostExists(id, out id2))
                {
                    filterContext.Result = controller.GetViewResult("NotFound", null);
                    return;
                }

                filterContext.RouteData.Values["id2"] = id2;
        }
    }
}